package com.pink;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicBoolean;

public class Assistant extends JFrame {
    public static void main(String[] args) throws AWTException {
        Robot robot = new Robot();
        JTextArea console = new JTextArea(10, 40);
        console.setFont(new Font("楷体", Font.BOLD, 16));
        console.setBackground(new Color(205, 205, 205));
        console.setLineWrap(true);
        JButton start = new JButton("开始");
        start.setSize(20, 10);
        AtomicBoolean s = new AtomicBoolean(false);
        Assistant a = new Assistant(console);
        a.setLayout(new FlowLayout());
        start.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                boolean b = s.get();

                if (!b) {
                    start.setText("停止");
                    s.set(true);
                    a.startScan();

                } else {
                    start.setText("稍等");
                    s.set(false);
                    a.stopScan();
                    start.setText("开始");
                }
            }

        });


        Dimension size = console.getPreferredScrollableViewportSize();
        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(110, 90, size.width, size.height);
        scrollPane.setViewportView(console);
        scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
        JPanel jPanel = new JPanel();
        jPanel.setSize(400, 300);
        jPanel.setLayout(new FlowLayout());
        jPanel.add(start);
        jPanel.add(scrollPane);
        a.add(jPanel);


        JPanel jp = new JPanel();
        jp.setSize(400, 50);
        jp.setBackground(Color.cyan);
        jp.setLayout(new FlowLayout());
//        jPanel.add();todo
        String[] titles = {"w: ", "h: "};


        JFormattedTextField w = new JFormattedTextField();
        w.setText("255");
        w.setMaximumSize(new Dimension(80, 50));
        w.setMinimumSize(new Dimension(80, 50));
        w.setPreferredSize(new Dimension(80, 50));

        jp.add(w);

        JFormattedTextField h = new JFormattedTextField();
        h.setText("255");
        h.setMaximumSize(new Dimension(80, 50));
        h.setMinimumSize(new Dimension(80, 50));
        h.setPreferredSize(new Dimension(80, 50));

        jp.add(h);

        JLabel out = new JLabel();
        JButton cal = new JButton("键盘K");
        cal.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e){

                    System.out.println("get k " + e.getClickCount()+
                            " ==== "+w.getText()+" ==== "+h.getText());

                    out.setText(robot.getPixelColor(
                            Integer.parseInt(w.getText()),
                            Integer.parseInt(h.getText())
                    ).toString()
                    );

            }


        });
        jp.add(cal);
        jp.add(out);
        a.add(jp);


        a.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        a.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosed(WindowEvent e) {
                super.windowClosed(e);
                a.getAb().set(false);
                a.getEs().shutdownNow();
            }
        });
        a.setVisible(true);
    }

    private ExecutorService es;
    private AtomicBoolean ab;
    private Robot robot;
    private JTextArea jTextArea;
    private Color initColor;
    private Integer i = 0;
    private CountDownLatch countDownLatch;

    public ExecutorService getEs() {
        return es;
    }

    public AtomicBoolean getAb() {
        return ab;
    }


    public Assistant(JTextArea jTextArea) throws AWTException {
        this.setTitle("显示鼠标的位置与颜色");

        this.setSize(600, 400);
        this.es = Executors.newSingleThreadExecutor();
        this.ab = new AtomicBoolean(true);
        this.robot = new Robot();
        this.jTextArea = jTextArea;
        this.initColor = jTextArea.getBackground();
    }

    public void startScan() {
        ab.set(true);
        es.submit(() -> {
            countDownLatch = new CountDownLatch(1);
            while (ab.get()) {
                robot.delay(1600);
                Point location = MouseInfo.getPointerInfo().getLocation();
                Color color = robot.getPixelColor(location.x, location.y);
                jTextArea.setBackground(color);
                i++;
                jTextArea.append(i + " : ");
                jTextArea.append(location.toString() + System.lineSeparator() + color.toString() + System.lineSeparator());
                jTextArea.setCaretPosition(jTextArea.getText().length());
            }
            countDownLatch.countDown();
        });
    }

    public void stopScan() {
        ab.set(false);
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
//        try {
//            TimeUnit.SECONDS.sleep(3);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        jTextArea.setBackground(initColor);
    }

}
